package com.shwread.android.qysw.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.NewActionHelp;
import com.google.gson.reflect.TypeToken;
import com.shwread.android.qysw10000209.R;
import com.shwread.android.ui.widget.LoadingView;
import com.shwread.http.base.ObjectHttpResponseHandler;
import com.squareup.picasso.Picasso;

import org.apache.http.Header;

import java.lang.reflect.Type;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import logic.bean.OrderDetailBean;
import logic.util.Utils;


/**
 * Created by lexy on 16/6/4.
 */
public class TicketOrderDetailActivity extends BaseBusinessActivity implements View.OnClickListener{


    private Button common_header_back;
    private TextView common_header_name;
    private TextView tvTicketOrderDetailName,tvTicketOrderDetailStatus,tvTicketOrderDetailProperty,
            tvTicketOrderDetailOriginalPrice,tvTicketOrderDetailOriginalCount,tvTicketOrderDetailBuyDate,
            tvTicketOrderDetailBuyPhone,tvTicketOrderDetailBuyAmount,tvTicketOrderDetailOrderNum,
            tvTicketOrderDetailOrderDate,tvTicketOrderDetailQrCodeMore;
    private ImageView ivTicketOrderDetailPic;
    private RelativeLayout rlTicketOrderDetailQrCode;
    private int orderId=-1;
    private int staNew=-1;
    private int staOld=-1;
    private LoadingView loadingView;


    @Override
    public Handler getUIHandler() {
        return null;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ticket_order_detail);
        initView();
        initListener();
        initDate();
    }

    private void initDate() {
        NewActionHelp.qryTicketOrderInfo(TicketOrderDetailActivity.this,orderId,new GetDataReq());
    }

    private void initListener() {
        rlTicketOrderDetailQrCode.setOnClickListener(this);
        tvTicketOrderDetailQrCodeMore.setOnClickListener(this);
        common_header_back.setOnClickListener(this);
    }

    private void initView() {
        //获取订单id
        orderId = this.getIntent().getIntExtra("orderId",-1);

        common_header_back= (Button) findViewById(R.id.common_header_back);
        common_header_back.setVisibility(View.VISIBLE);
        common_header_name = (TextView) findViewById(R.id.common_header_name);
        common_header_name.setVisibility(View.VISIBLE);
        common_header_name.setText("订单详情");
        tvTicketOrderDetailName = (TextView) findViewById(R.id.tvTicketOrderDetailName);
        tvTicketOrderDetailStatus = (TextView) findViewById(R.id.tvTicketOrderDetailStatus);
        tvTicketOrderDetailProperty = (TextView) findViewById(R.id.tvTicketOrderDetailProperty);
        tvTicketOrderDetailOriginalPrice = (TextView) findViewById(R.id.tvTicketOrderDetailOriginalPrice);
        tvTicketOrderDetailOriginalCount = (TextView) findViewById(R.id.tvTicketOrderDetailOriginalCount);
        tvTicketOrderDetailBuyDate = (TextView) findViewById(R.id.tvTicketOrderDetailBuyDate);
        tvTicketOrderDetailBuyPhone = (TextView) findViewById(R.id.tvTicketOrderDetailBuyPhone);
        tvTicketOrderDetailBuyAmount = (TextView) findViewById(R.id.tvTicketOrderDetailBuyAmount);
        tvTicketOrderDetailOrderNum = (TextView) findViewById(R.id.tvTicketOrderDetailOrderNum);
        tvTicketOrderDetailOrderDate = (TextView) findViewById(R.id.tvTicketOrderDetailOrderDate);
        tvTicketOrderDetailQrCodeMore = (TextView) findViewById(R.id.tvTicketOrderDetailQrCodeMore);
        ivTicketOrderDetailPic = (ImageView) findViewById(R.id.ivTicketOrderDetailPic);
        rlTicketOrderDetailQrCode = (RelativeLayout) findViewById(R.id.rlTicketOrderDetailQrCode);

        loadingView = (LoadingView)findViewById(R.id.my_goods_loading_view);
        loadingView.setLoadingAnim(R.drawable.home_progress);
        loadingView.setVisibility(View.VISIBLE);
        loadingView.changeLoadingStatus(0);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.common_header_back:
                if (staNew!=-1) {
                    Intent in = new Intent();
                    in.putExtra("status",staNew);
                    setResult(2000,in);
                }
                this.finish();
                break;
            case R.id.tvTicketOrderDetailQrCodeMore:
            case R.id.rlTicketOrderDetailQrCode:
                Intent intent = new Intent(TicketOrderDetailActivity.this,QrCodeActivity.class);
                intent.putExtra("orderId",orderId);
                startActivityForResult(intent,989);
                break;
        }
    }

    @Override
    public void onBackPressed() {
        if (staNew!=-1) {
            Intent in = new Intent();
            in.putExtra("status",staNew);
            setResult(2000,in);
        }
        this.finish();
        super.onBackPressed();
    }

    private class GetDataReq
            extends
            ObjectHttpResponseHandler<OrderDetailBean> {
        @Override
        public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
            loadingView.setVisibility(View.GONE);
            loadingView.changeLoadingStatus(2);
            if (statusCode < 1000) {

                Utils.ToastShow(TicketOrderDetailActivity.this, "网络错误");
            }
            else {
                Utils.ToastShow(TicketOrderDetailActivity.this, "请求出错");
            }
        }
        @Override
        public void onSuccess(int statusCode, Header[] headers, OrderDetailBean obj, String responseString) {
            loadingView.setVisibility(View.GONE);
            loadingView.changeLoadingStatus(2);
            Log.e("订单详情数据",obj.toString());
            Picasso.with(TicketOrderDetailActivity.this).load(obj.getTicketImg()).into(ivTicketOrderDetailPic);
            tvTicketOrderDetailName.setText(obj.getSpotName());
            String status = obj.getStatus();
            if (status.equals("10")){
                tvTicketOrderDetailStatus.setText("待使用");
                tvTicketOrderDetailStatus.setTextColor(Color.parseColor("#ff8000"));
                tvTicketOrderDetailOriginalCount.setText("数量："+obj.getUsableAmount());
            }else if (status.equals("20")){
                tvTicketOrderDetailStatus.setText("已使用");
                tvTicketOrderDetailStatus.setTextColor(Color.parseColor("#f1040f"));
                tvTicketOrderDetailOriginalCount.setText("数量："+(obj.getAmount()-obj.getUsableAmount()));
            }else if(status.equals("30")){
                tvTicketOrderDetailStatus.setText("已过期");
                tvTicketOrderDetailStatus.setTextColor(Color.parseColor("#1BA9FB"));
                tvTicketOrderDetailOriginalCount.setText("数量："+obj.getUsableAmount());
            }else if(status.equals("40")){//还没有出码
                tvTicketOrderDetailStatus.setText("待使用");
                tvTicketOrderDetailStatus.setTextColor(Color.parseColor("#ff8000"));
                tvTicketOrderDetailOriginalCount.setText("数量："+obj.getUsableAmount());
            }
            tvTicketOrderDetailBuyAmount.setText("购买数量："+obj.getAmount());
            tvTicketOrderDetailProperty.setText(obj.getPropertyName());
            DecimalFormat df = new DecimalFormat("0.0");
            tvTicketOrderDetailOriginalPrice.setText("总价：￥"+df.format(obj.getTotalMoney()/100));
            tvTicketOrderDetailBuyDate.setText("预约日期："+formatDate(obj.getOrderTime()));
            if (null!=obj.getMobilePhone()&&obj.getMobilePhone().length()!=0){
                tvTicketOrderDetailBuyPhone.setText("购买手机号："+obj.getMobilePhone());
            }else{
                tvTicketOrderDetailBuyPhone.setVisibility(View.GONE);
            }
            tvTicketOrderDetailOrderNum.setText("订单号："+obj.getOrderNo());
            tvTicketOrderDetailOrderDate.setText("订单日期："+formatDate(obj.getCreateTime()));
        }
        @Override
        public Type getType() {
            return new TypeToken<OrderDetailBean>() {
            }.getType();
        }
    }

    private String formatDate(long dateTime){
        Date date = new Date(dateTime);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(date);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode==989){
            initDate();
        }
    }
}
